package com.mdlib.droid.module.home.fragment;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.mdlib.droid.R;
import com.mdlib.droid.base.BaseAppFragment;
import com.mdlib.droid.model.entity.HomeLuckEntity;
import com.mdlib.droid.module.home.adapter.HomeContentAdapter;
import com.mdlib.droid.module.home.adapter.HomeLuckAdapter;
import com.mdlib.droid.util.LogUtil;
import com.mdlib.droid.util.ShadowDrawable;
import com.mdlib.droid.widget.MyScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;

/**
 * 作者：HQY on 2018/10/22 14:07
 * 邮箱：hqy_xz@126.com
 */

public class HomeFragment extends BaseAppFragment {


    @BindView(R.id.ll_home_top)
    LinearLayout mLlHomeTop;
    @BindView(R.id.rv_home_content)
    RecyclerView mRvHomeContent;
    @BindView(R.id.rv_home_luck)
    RecyclerView mRvHomeLuck;
    @BindView(R.id.tv_home_title)
    TextView mTvHomeTitle;
    @BindView(R.id.rl_home_title)
    RelativeLayout mRlHomeTitle;
    @BindView(R.id.rl_home_top)
    RelativeLayout mRlHomeTop;
    @BindView(R.id.msv_home_all)
    MyScrollView mMsvHomeAll;
    @BindArray(R.array.home_content_text)
    String[] mContents;
    @BindArray(R.array.home_luck_text)
    String[] mLucks;
    private List<String> mContentList = new ArrayList<>();
    private HomeContentAdapter mHomeContentAdapter;

    private List<HomeLuckEntity> mHomeLuckList = new ArrayList<>();
    private HomeLuckAdapter mHomeLuckAdapter;

    private int mTitleHieght;

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        getBarHeight();

        mRlHomeTop.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mRlHomeTop.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                LogUtil.e("标题高度：" + mRlHomeTop.getHeight());

            }
        });

        for (int i = 0; i < mContents.length; i++) {
            mContentList.add(mContents[i]);
        }
        mHomeContentAdapter = new HomeContentAdapter(mContentList);
        mRvHomeContent.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mRvHomeContent.setAdapter(mHomeContentAdapter);
        mRvHomeContent.setNestedScrollingEnabled(false);
        for (int i = 0; i < mLucks.length; i++) {
            HomeLuckEntity luck = new HomeLuckEntity();
            if (i == 0) {
                luck.setNum(0.3f);
            } else if (i == 2) {
                luck.setNum(0.55f);
            } else {
                luck.setNum(0.49f);
            }
            luck.setName(mLucks[i]);
            mHomeLuckList.add(luck);
        }

        mHomeLuckAdapter = new HomeLuckAdapter(mHomeLuckList);
        mRvHomeLuck.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mRvHomeLuck.setAdapter(mHomeLuckAdapter);
        mRvHomeLuck.setNestedScrollingEnabled(false);
        ShadowDrawable.setShadowDrawable(mLlHomeTop, getActivity().getResources().getColor(R.color.white), dpToPx(10),
                Color.parseColor("#99d2dad7"), dpToPx(8), 0, dpToPx(6));

        getSv();
    }

    private void getSv() {
        mMsvHomeAll.setOnScrollListener(new MyScrollView.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                if (scrollY <= 0) {
                    ImmersionBar.with(getActivity())
                            .statusBarDarkFont(false, 0)
                            .init();
                    mRlHomeTop.setVisibility(View.GONE);
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_one));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_one));
                } else if (scrollY <= 25) {
                    mRlHomeTop.setVisibility(View.VISIBLE);
                    ImmersionBar.with(getActivity())
                            .statusBarDarkFont(true, 0.2f)
                            .init();
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_one));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_one));
                } else if (scrollY <= 50) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_two));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_two));
                } else if (scrollY <= 75) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_three));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_three));
                } else if (scrollY <= 100) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_four));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_four));
                } else if (scrollY <= 125) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_five));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_five));
                } else if (scrollY <= 150) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_six));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_six));
                } else if (scrollY <= 175) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_seven));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_seven));
                } else if (scrollY <= 200) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_eight));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_eight));
                } else if (scrollY <= 225) {
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white_nine));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.reveal_nine));
                } else {
                    ImmersionBar.with(getActivity())
                            .statusBarDarkFont(true, 0.2f)
                            .init();
                    mRlHomeTop.setVisibility(View.VISIBLE);
                    mRlHomeTop.setBackgroundColor(getActivity().getResources().getColor(R.color.white));
                    mTvHomeTitle.setTextColor(getActivity().getResources().getColor(R.color.black));
                }
            }

            @Override
            public void scrollOritention(int oritention) {

            }
        });

    }


    private int dpToPx(int dp) {
        return (int) (Resources.getSystem().getDisplayMetrics().density * dp + 0.5f);
    }

    /**
     * 获取状态栏高度——方法1
     */
    private void getBarHeight() {
        int statusBarHeight1 = -1;
//获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mRlHomeTitle.getLayoutParams();
        layoutParams.setMargins(0, statusBarHeight1, 0, 0);
        mRlHomeTitle.setLayoutParams(layoutParams);
    }


}
